<template>
  <div class="v-time-point g-jc-sb">
    <el-input
      v-model="value"
      :disabled="isModelDisabled"
      readonly
    ></el-input>
    <div
      v-if="selected"
      class="g-pd-r-4 img-btn"
    >
      <img
        src="statics/icons/app/iconmore.png"
        class="g-pointer"
        @click="handleClick"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { DateAndTimeSettingWindow } from "@/config/customSystemConfig/customWindows/dateAndTimeSettingWindow";
import { computed, inject, Ref } from "vue";
import { PropertyRow } from "../../types";
import { modelService } from "@/modules/model/service";

const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
}>();
const isModelDisabled = inject("isModelDisabled") as Ref<boolean>;
const projectId = app.activeProject!.config.id;
// const timelineTypeId = '16371555332288';

const value = computed(() => (props.row.valueSpecificationInfo?.value as string) || "");
const selected = computed(() => props.selected);

async function handleClick() {
  // const data = await archService.queryInstanceByType(projectId, timelineTypeId);
  // if (!data?.length) {
  //   app.$notify({
  //     title: '项目缺少<<时间线>>模型',
  //     message: '项目中没有可使用的时间线模型，请在根目录或全部视图目录下创建时间线模型',
  //     type: 'warning'
  //   });
  //   return;
  // }
  // await timePointModal.popup({
  //   timelines: data,
  //   datetimeModelId: props.row.value as string || '',
  //   projectId,
  //   row: props.row
  // });
  DateAndTimeSettingWindow.popup({ projectId, date: props.row.valueSpecificationInfo?.value }, async ({ success, result }) => {
    let modelId = props.row.modelId;
    if (success) {
      const { timeLine, date } = result;
      let propertyKey = props.row.key;
      if (propertyKey === "ActualMeasuresTable-StartDate" || propertyKey === "ActualMeasuresTable-EndDate") {
        const model = await modelService.getModelData(projectId, modelId);
        modelId = model.ownerId;
      }
      if (propertyKey === "ActualMeasuresTable-StartDate") {
        propertyKey = "UAF::Architecture Management::Taxonomy::ActualState::startDate";
      }
      if (propertyKey === "ActualMeasuresTable-EndDate") {
        propertyKey = "UAF::Architecture Management::Taxonomy::ActualState::endDate";
      }
      await modelService.setTimeLine(projectId, modelId, timeLine, propertyKey, date);
    }
    DateAndTimeSettingWindow.done();
  });
}
</script>

<style lang="scss">
.v-time-point {
  .el-input__wrapper {
    background-color: unset;
  }
  > .img-btn {
    display: flex;
    align-items: center;
  }
}
</style>
